package com.crm.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Returnmoney;
import com.crm.services.CustomerServices;
import com.crm.services.OrderInfoServers;
import com.crm.services.ReturnMoneyService;
import com.crm.services.UserServices;
import com.github.pagehelper.PageInfo;

@Controller
public class ReturnMoneyController {
	
	
	@Autowired 
	ReturnMoneyService rmService;

	@Autowired
	UserServices userServer;
	@Autowired
	OrderInfoServers orderServer;

	@Autowired
	CustomerServices cumService;
	
	/**
	 * 分页跳转到回款记录
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/sales/prosale/returnMoney")
	public ModelAndView rmMoney(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("sales/prosale/returnMoney");
		//分页查询订单
		List<Map<String, String>> findAllMaper = rmService.selectByPager(pageNum, pageSize,statusid,search);
		System.out.println(findAllMaper);
		String jsonString = JSONObject.toJSONString(findAllMaper);
		//获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(findAllMaper);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);		
		return mv;
	}
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/sales/prosale/returnMoney")
	@ResponseBody	
	public PageInfo<Map<String, String>> backMoneyPlan(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = rmService.selectByPager(pageNum, pageSize,statusid,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}
	
	/**
	 * 增加回款记录
	 * @param comcustid
	 * @param orderid
	 * @param date1
	 * @param bmpmoney
	 * @param radiosatus
	 * @param uid
	 * @param textcommunication
	 * @return
	 */
	@PostMapping("/sales/prosale/addrm")
	@ResponseBody
	public String addOrder(@RequestParam(name = "ID", required = false) String id,
			@RequestParam(name = "comcustid", required = false) Integer comcustid,
			@RequestParam(name = "orderid", required = false) Integer orderid,
			@RequestParam(name = "date1", required = false) String date1,
			@RequestParam(name = "rmmoney", required = false) String rmmoney,
			@RequestParam(name = "rmpay", required = false) Integer rmpay,
			@RequestParam(name = "rmsort", required = false) Integer rmsort,
			@RequestParam(name = "uid", required = false) Integer uid,
			@RequestParam(name = "rmremark", required = false) String rmremark) {
		System.out.println("我是ID"+id);
		// 根据ID查询客户
		Orderinfo orderinfo = orderServer.findById(orderid);
		Returnmoney rm=new Returnmoney();
		rm.setCustid(comcustid);
		rm.setOrderinfo(orderinfo);
		rm.setRmDate(java.sql.Date.valueOf(date1.substring(0, 10)));
		rm.setRmMoney(new BigDecimal(rmmoney));
		rm.setRmPay(rmpay);
		rm.setRmSort(rmsort);
		rm.setRmRemark(rmremark);
		rm.setRmStatus(1102);
		rm.setUid(uid);
		Returnmoney addrm = rmService.addBmp(rm,orderid,id);
		
		System.out.println(addrm);
		return "新增回款记录";
	}

	
	/**
	 * 用于查询加载回款记录界面数据
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/addrmpage")
	@ResponseBody
	public JSONObject addOrderpage() {
		//添加回款记录分类数据
		List<Map<String, String>> selectBySort = rmService.selectBySort();
		
		//添加回款记录付款方式数据
		List<Map<String, String>> selectByPay = rmService.selectByPay();
		// 用于加载客户信息
		List<Map<String, String>> selectAllCustomer = cumService.selectAllCustomer();
		// 用于加载所有者信息
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		System.out.println(selectAllCustomer);
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("customer", selectAllCustomer);
		jsonarray.put("userAll", selectUserAll);
		jsonarray.put("pay", selectByPay);
		jsonarray.put("sort", selectBySort);
		return jsonarray;
	}
	
	/**
	 * 点击跳转修改页，用于界面的数据显示
	 * 
	 * @param comcustid
	 * @return
	 */
	@PostMapping("/sales/prosale/updatermpage")
	@ResponseBody
	public JSONObject updatebmppage(@RequestParam("ID") Integer rmid) {
		//添加回款记录分类数据
		List<Map<String, String>> selectBySort = rmService.selectBySort();
		//添加回款记录付款方式数据
		List<Map<String, String>> selectByPay = rmService.selectByPay();
		// 加载页面数据
		List<Map<String, String>> findById = rmService.findById(rmid);
		// 用于加载客户信息
		List<Map<String, String>> selectAllCustomer = cumService.selectAllCustomer();
		// 用于加载所有者信息
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("customer", selectAllCustomer);
		jsonarray.put("userAll", selectUserAll);
		jsonarray.put("pay", selectByPay);
		jsonarray.put("sort", selectBySort);
		jsonarray.put("rmform", findById);
		return jsonarray;
	}
	
	/**
	 * 用于查询加载回款记录界面数据订单
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/rmorderpage")
	@ResponseBody
	public JSONObject bmporderpage(@RequestParam("ID") Integer cumid) {
		// 用于加载所有者信息
		List<Map<String, String>> selectAllBypageID = orderServer.selectAllBypageID(cumid);
		JSONObject jsonarray = new JSONObject();
		System.out.println(selectAllBypageID);
		jsonarray.put("order", selectAllBypageID);
		return jsonarray;
	}
	
	/**
	 * 增加回款记录
	 * @param comcustid
	 * @param orderid
	 * @param date1
	 * @param bmpmoney
	 * @param radiosatus
	 * @param uid
	 * @param textcommunication
	 * @return
	 */
	@PostMapping("/sales/prosale/updaterm")
	@ResponseBody
	public String updaterm(@RequestParam(name = "ID", required = false) String id,
			@RequestParam(name = "comcustid", required = false) Integer comcustid,
			@RequestParam(name = "orderid", required = false) Integer orderid,
			@RequestParam(name = "date1", required = false) String date1,
			@RequestParam(name = "rmmoney", required = false) String rmmoney,
			@RequestParam(name = "rmpay", required = false) Integer rmpay,
			@RequestParam(name = "rmsort", required = false) Integer rmsort,
			@RequestParam(name = "uid", required = false) Integer uid,
			@RequestParam(name = "rmremark", required = false) String rmremark) {
		System.out.println("我是ID"+id);
		// 根据ID查询客户
		Orderinfo orderinfo = orderServer.findById(orderid);
		Returnmoney rm=new Returnmoney();
		rm.setCustid(comcustid);
		rm.setOrderinfo(orderinfo);
		rm.setRmDate(java.sql.Date.valueOf(date1.substring(0, 10)));
		rm.setRmMoney(new BigDecimal(rmmoney));
		rm.setRmPay(rmpay);
		rm.setRmSort(rmsort);
		rm.setRmRemark(rmremark);
		rm.setRmStatus(1102);
		rm.setUid(uid);
		Returnmoney addrm = rmService.addBmp(rm,orderid,id);
		
		System.out.println(addrm);
		return "新增回款记录";
	}
	
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping("/sales/prosale/deleterm")
	@ResponseBody
	public String dateleOrder(@RequestParam("ID") Integer id) {
		// 调用删除
		rmService.deleteRm(id);
		return "删除";

	}
}
